<?php

/**
 * 法拍百科
 */

namespace app\api\controller;


use app\api\model\Wiki as wikiModel;
use app\api\model\WikiGive;
use app\common\controller\ApiController;
use think\Db;
use think\Exception;

class Wiki extends ApiController {

//    protected $notNeedToken = ['getlist'];
    protected $notNeedPhone = ['getlist'];

    /**
     * 法拍百科
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $data = wikiModel::getList($params, $this->headers);
            return $this->resData('success', ['page' => $data[2], 'pageNum' => $data[3], 'count' => $data[1], 'data' => $data[0]]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 点赞
     * @return false|string
     */
    public function dianZan() {
        try {
            Db::transaction(function () {
                $params = $this->checkParams('wiki_id');
                if ($params === false) {
                    return $this->resErr();
                }
                $is_zan = WikiGive::see($params);
                //添加
                if ($is_zan == 1) {
                    wikiModel::add($params);
                }
                //删除
                if ($is_zan == 2) {
                    wikiModel::del($params);
                }
            });
            return $this->resData('操作成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
}
